package com.niodata.dp.core.usermgr.dao;

import com.niodata.dp.core.usermgr.model.TokenInfo;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TokenInfoMapper {

  @Select("select * from TOKEN_INFO WHERE USER_ID=#{userId}")
  @ResultMap("tokenInfoResult")
  List<TokenInfo> getUserTokenList(@Param("userId") Long userId);

  @Select("select * from TOKEN_INFO where TOKEN=#{token}")
  @ResultMap("tokenInfoResult")
  TokenInfo getByTokenId(@Param("token") String token);

  @Update("update TOKEN_INFO set REFRESH_TIME=#{token.refreshTime},"
        + "EXPIRE_TIME=#{token.expireTime},BIND_IP=#{token.bindIp},ACTIVE=#{token.active} "
        + "where TOKEN=#{token.token}")
  void updateToken(@Param("token") TokenInfo token);

  @Insert("insert into TOKEN_INFO(USER_ID,TOKEN,BIND_IP,REFRESH_TIME,"
        + "EXPIRE_TIME,ACTIVE,SERVICES,CREATE_TIME)"
        + "VALUES(#{token.userId},#{token.token},#{token.bindIp},#{token.refreshTime},"
        + "#{token.expireTime},#{token.active},#{token.services},#{token.createTime})")
  void insert(@Param("token") TokenInfo tokenInfo);

  @Delete("delete from TOKEN_INFO WHERE TOKEN=#{token}")
  void delete(@Param("token") String token);
}
